﻿using System;
using System.Linq;
using System.Windows.Forms;
using ScrapySharp.Extensions;
using V5_Apps.Core;
using V5_Apps.Plugins.Qvod._Class;
using V5_Apps.Plugins.Qvod._Class.Model;
using XmlDatabase.Core;
namespace V5_Apps.Plugins.Qvod {
    public partial class frmTest : Form {
        public frmTest() {
            InitializeComponent();
        }

        private void Bind_NewModule() {
            this.txtCssUrl.Text = string.Empty;
            this.txtUrlContent.Text = string.Empty;
            this.txtCssName.Text = string.Empty;
            this.txtCssSelect.Text = string.Empty;
            this.txtCssAttr.Text = string.Empty;
            this.txtCssInclude.Text = string.Empty;
            this.txtCssOutclude.Text = string.Empty;
            this.rbtnCssIsHttp2.Checked = false;
            this.cmbReg.Text = "匹配";
            this.txtCssGuid.Text = string.Empty;
        }
        #region 测试模块
        private ModelSpiderModule SpiderModel = new ModelSpiderModule();
        private string HtmlContent = string.Empty;
        private bool isReg = false;
        string encode = string.Empty;
        private void btnTestCssContent_Click(object sender, EventArgs e) {
            if (string.IsNullOrEmpty(this.txtUrlContent.Text)) {
                this.txtUrlContent.Text = HttpHelper.Instance.GetHttpPage(this.txtCssUrl.Text, ref encode, 60000);
            }
            this.txtLogView.Clear();
            //
            string cssUrl = this.txtCssUrl.Text;
            HtmlContent = this.txtUrlContent.Text;
            string cssSelect = this.txtCssSelect.Text;
            var cssAttr = this.txtCssAttr.Text;
            var cssInclude = this.txtCssInclude.Text;
            var cssIsHttp = this.rbtnCssIsHttp2.Checked ? true : false;
            isReg = this.cmbReg.Text == "匹配" ? true : false;
            SpiderModel = new ModelSpiderModule() {
                CssUrl = cssUrl,
                CssSelect = cssSelect,
                CssAttr = cssAttr,
                CssInclude = cssInclude,
                CssIsHttp = cssIsHttp
            };
            ThreadMultiHelper tm = new ThreadMultiHelper(1);
            tm.WorkMethod = new ThreadMultiHelper.DelegateWork(WorkMethod);
            tm.CompleteEvent = new ThreadMultiHelper.DelegateComplete(CompleteEvent);
            tm.Start();
        }


        private void WorkMethod(int taskindex, int threadindex) {
            string cssUrl = SpiderModel.CssUrl;
            string cssHtmlContent = HtmlContent;
            string cssSelect = SpiderModel.CssSelect;
            var cssAttr = SpiderModel.CssAttr;
            var cssInclude = SpiderModel.CssInclude;
            var cssIsHttp = SpiderModel.CssIsHttp;
            var cssIsReg = isReg;
            //
            string parseUrl = QvodHelper.Instance.ToUrl(SpiderModel, cssHtmlContent, ref encode, cssUrl, cssIsReg);

            if (string.IsNullOrEmpty(parseUrl)) {
                OutMessage("没有获取到InnerHtml");
                return;
            }
            OutMessage(parseUrl);
        }

        private void CompleteEvent() {
            OutMessage("数据获取完成!");
        }

        //输出信息
        private void OutMessage(string Msg) {
            if (this.txtLogView.InvokeRequired) {
                this.txtLogView.Invoke(new MethodInvoker(delegate() {
                    this.txtLogView.AppendText(Msg);
                    this.txtLogView.AppendText("\r\n");
                }));
            }
            else {
                this.txtLogView.AppendText(Msg);
                this.txtLogView.AppendText("\r\n");
            }
        }
        #endregion

        #region 保存模块
        private void btnSaveTest_Click(object sender, EventArgs e) {
            string CssGuid = this.txtCssGuid.Text;
            string CssUrl = this.txtCssUrl.Text;
            string CssName = this.txtCssName.Text;
            string CssSelect = this.txtCssSelect.Text;
            string CssAttr = this.txtCssAttr.Text;
            string CssInclude = this.txtCssInclude.Text;
            string CssOutclude = this.txtCssOutclude.Text;
            bool CssIsHttp = this.rbtnCssIsHttp2.Checked ? true : false;
            ModelSpiderModule model = new ModelSpiderModule();
            using (XDatabase db = XDatabase.Open(AppHelper.Instance.GetAppPath("Qvod") + "Spider")) {
                var query = db.Query<ModelSpiderModule>();
                if (query != null && query.Where(p => p.CssGuid == CssGuid && CssGuid != string.Empty).FirstOrDefault() != null) {
                    model = query.Where(p => p.CssGuid == CssGuid && CssGuid != string.Empty).FirstOrDefault();
                    model.CssUrl = CssUrl;
                    model.CssName = CssName;
                    model.CssSelect = CssSelect;
                    model.CssAttr = CssAttr;
                    model.CssInclude = CssInclude;
                    model.CssOutclude = CssOutclude;
                    model.CssIsHttp = CssIsHttp;
                    model.UpdateTime = DateTime.Now;
                    db.Store(model);
                }
                else {
                    if (query != null && query.FirstOrDefault(p => p.CssName == CssName) != null) {
                        MessageBox.Show("已经存在改模块!");
                        return;
                    }
                    else {
                        model = new ModelSpiderModule() {
                            CssUrl = CssUrl,
                            CssName = CssName,
                            CssSelect = CssSelect,
                            CssAttr = CssAttr,
                            CssInclude = CssInclude,
                            CssOutclude = CssOutclude,
                            CssIsHttp = CssIsHttp,
                            UpdateTime = DateTime.Now
                        };
                        db.Store(model);
                    }
                }
            }

            this.Bind_DataList();

        }
        #endregion

        #region 加载模块

        private void Bind_DataList() {
            using (XDatabase db = XDatabase.Open(AppHelper.Instance.GetAppPath("Qvod") + "Spider")) {
                var query = db.Query<ModelSpiderModule>();
                if (query != null) {
                    var list = query.ToList();
                    list.Sort(delegate(ModelSpiderModule x, ModelSpiderModule y) {
                        return y.UpdateTime.CompareTo(x.UpdateTime);
                    });
                    this.listBoxCssSelect.DataSource = list;
                    this.listBoxCssSelect.DisplayMember = "CssName";
                }
            }
        }

        #endregion

        private void listBoxCssSelect_SelectedIndexChanged(object sender, EventArgs e) {
            var model = this.listBoxCssSelect.SelectedItem as ModelSpiderModule;
            if (model != null) {
                this.txtCssGuid.Text = model.CssGuid;
                this.txtCssUrl.Text = model.CssUrl;
                this.txtCssName.Text = model.CssName;
                this.txtCssSelect.Text = model.CssSelect;
                this.txtCssAttr.Text = model.CssAttr;
                this.txtCssInclude.Text = model.CssInclude;
                this.txtCssOutclude.Text = model.CssOutclude;
                if (model.CssIsHttp) {
                    this.rbtnCssIsHttp2.Checked = true;
                }
                else {
                    this.rbtnCssIsHttp1.Checked = true;
                }
            }
        }

        private void frmTest_Load(object sender, EventArgs e) {
            this.Bind_DataList();
        }

        private void btnNewModule_Click(object sender, EventArgs e) {
            this.Bind_NewModule();
        }



    }
}
